package com.haier.npt.sso.api;


import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.haier.npt.common.JsonResult;
import com.haier.npt.exception.BizBaseException;
import com.haier.npt.security.domain.SysApp;
import com.haier.npt.sso.domain.HaierUser;
import com.haier.npt.sso.i18n.SSOExceptionCode;
import com.haier.npt.sso.oauth.OauthResult;
import com.haier.npt.sso.oauth.OauthService;
import com.haier.npt.sso.resources.LoginResource;

/**
 *
 * @author lixu
 */
@RestController
@RequestMapping("/user")
public class LoginController implements LoginResource {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private OauthService oauthService;



    /**
     * 登录接口
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public JsonResult<OauthResult> login(@RequestHeader(name = "Authorization") String header, @RequestParam("username") String username, @RequestParam("password") String password) {
        OauthResult result = oauthService.login(header, username, password);
        return JsonResult.success(result);
    }

    /**
     * 通过appKey和token模拟登陆
     * @param accessToken
     * @param appKey
     * @return
     */
    @Override
    @RequestMapping(value = "/simulatedLogin", method = RequestMethod.POST)
    public JsonResult<SysApp> simulatedLogin(@RequestParam("access_token") String accessToken, @RequestParam("app_key") String appKey) {
        SysApp sysApp = oauthService.login(accessToken, appKey);
        if (null == sysApp) {
            throw new BizBaseException(SSOExceptionCode.TOKEN_INVALID);
        }
        return JsonResult.success(sysApp);
    }

}
